import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Login from "../views/Login.vue";
Vue.use(VueRouter);

const routes = [
  {
    path: "/home",
    name: "Home",
    hidden: true,
    component: Home
  },
  {
    path: "/login",
    name: "Login",
    hidden: true,
    component: Login
  },
  {
    path: "/home",
    name: "About",
    component: Home,
    hidden: true,
    children: [
      {
        path: "/about",
        name: "About",
        hidden: false,
        meta: { title: "个人中心", keepAlive: true },
        component: () => import("../views/About.vue")
      }
    ]
  },
  {
    path: "/home",
    name: "User",
    hidden: false,
    component: Home,
    meta: { title: "用户管理", icon: "el-icon-setting" },
    children: [
      {
        path: "/users",
        name: "UserManager",
        hidden: false,
        meta: { title: "用户管理", keepAlive: true },
        component: () => import("../views/User.vue")
      }
    ]
  },
  {
    path: "/home",
    name: "Category",
    hidden: false,
    component: Home,
    meta: { title: "栏目管理", icon: "el-icon-notebook-2" },
    children: [
      {
        path: "/category",
        name: "CategoryManager",
        hidden: false,
        meta: { title: "栏目管理", keepAlive: true },
        component: () => import("../views/Category.vue")
      }
    ]
  },
  {
    path: "/home",
    name: "Tags",
    hidden: false,
    component: Home,
    meta: { title: "Tag管理", icon: "el-icon-collection-tag", keepAlive: true },
    children: [
      {
        path: "/tags",
        name: "TagsManager",
        hidden: false,
        meta: { title: "Tag管理", keepAlive: true },
        component: () => import("../views/Tags.vue")
      }
    ]
  },
  {
    path: "/home",
    name: "Article",
    hidden: false,
    component: Home,
    meta: { title: "文章管理", icon: "el-icon-edit-outline" },
    children: [
      {
        path: "/article",
        name: "ArticleManager",
        hidden: false,
        meta: { title: "文章管理", keepAlive: false },
        component: () => import("../views/Article.vue")
      },
      {
        path: "/edit",
        name: "EditArticle",
        hidden: false,
        component: () => import("../views/WriteArticle.vue"),
        meta: { title: "写文章", keepAlive: false }
      },
      {
        path: "/details",
        name: "ArticleDetails",
        hidden: true,
        component: () => import("../views/ArticleDetails.vue"),
        meta: { title: "文章详情", keepAlive: false }
      }
    ]
  }
];

const router = new VueRouter({
  // mode: "history",
  // base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  let isAuthenticated = localStorage.getItem("userInfo") != null ? true : false;
  if (to.name !== "Login" && !isAuthenticated) next({ name: "Login" });
  else next();
});

export default router;
